<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise <jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 小区模型
 * @author Cruise <jiangyu@hrbjjhh.com>
 */
class VillageModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
    	array('province_id', 'checkId', '请选择省份', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT),
    	array('city_id', 'checkId', '请选择城市', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT),
    	array('ward_id', 'checkId', '请选择行政区', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT), 
    	array('village_name', 'require', '小区不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	//array('into_time', 'require', '入户时间不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
    	array('village_name', '', '小区已经存在', self::EXISTS_VALIDATE, 'unique'),
    	array('village_name', '1,30', '小区长度不能超过30个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', NOW_TIME, self::MODEL_INSERT),
    	array('update_time', NOW_TIME, self::MODEL_UPDATE),
    	//array('province_id','1',self::MODEL_INSERT),
    	array('status', '1', self::MODEL_INSERT),
    	array('into_time', 'getTime', self::MODEL_BOTH, 'callback'),
    );

    /**
     * @todo 检测名称是否存在
     * @param  string $village_name 小区名称
     * @return boolean     ture - 未重名，false - 已重名
     */
    protected function checkName($village_name){
    	$village_id = (int)I('village_id');
    	//判断是否存在
    	$map = array();
    	if($village_name != 0){
    		$map['village_id'] = array('neq',$village_id);
    	}
    	$map['village_name'] = trim($village_name);
    	$map['status'] = array('gt',-1);
    	if($this->where($map)->find()){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 检测省市区联动是否成立
     * @param  string $id 省市区id
     * @return boolean     ture - id成立，false - id无效
     */
    protected function checkId($id){
    	if($id == 0 || $id == ''){
    		return false;
    	}else{
    		return true;
    	}
    }
    
    /**
     * @todo 小区详情
     * @param int $village_id 小区id
     * @param string $field 查询字段
     * @return false|array
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function VillageShow($village_id = 0,$field = ''){
    	//获取数据
    	$village_id = (int)$village_id;
    	if($village_id == 0){
    		$this->error = '请正确操作';
    		return false;
    	}
    	$map = array();//初始化查询条件
    	//查询条件
    	$map['village_id'] = $village_id;
    	$map['status'] = array('gt',-1);
    	//返回值
    	return $this->where($map)->field($field)->find();
    }
       
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
     */
    public function getPk(){
    	return 'village_id';
    }
}
